import re
from datetime import datetime
from typing import List, Tuple

class DataValidator:
    """数据验证工具类"""
    
    @staticmethod
    def validate_draw_number(draw_number: str) -> bool:
        """验证期数格式"""
        if not draw_number:
            return False
        # 期数格式：年份(4位) + 期数(3位)，如2025001
        pattern = r'^\d{7}$'
        return bool(re.match(pattern, draw_number))
    
    @staticmethod
    def validate_ball_numbers(red_balls: List[int], blue_ball: int) -> bool:
        """验证球号范围"""
        if not red_balls or len(red_balls) != 6:
            return False
        
        # 验证红球范围(1-33)且不重复
        red_set = set(red_balls)
        if len(red_set) != 6:
            return False
        
        for ball in red_balls:
            if not (1 <= ball <= 33):
                return False
        
        # 验证蓝球范围(1-16)
        if not (1 <= blue_ball <= 16):
            return False
        
        return True
    
    @staticmethod
    def validate_date_range(start_date: str, end_date: str) -> bool:
        """验证日期范围"""
        try:
            start = datetime.strptime(start_date, '%Y-%m-%d')
            end = datetime.strptime(end_date, '%Y-%m-%d')
            return start <= end
        except ValueError:
            return False
    
    @staticmethod
    def validate_analysis_params(start_draw: str, end_draw: str, data_count: int = 0) -> Tuple[bool, str]:
        """验证分析参数"""
        if not start_draw or not end_draw:
            return False, "开始期数和结束期数不能为空"
        
        if not DataValidator.validate_draw_number(start_draw):
            return False, "开始期数格式无效"
        
        if not DataValidator.validate_draw_number(end_draw):
            return False, "结束期数格式无效"
        
        if data_count > 100:
            return False, "分析数据量过大，请选择不超过100期的数据"
        
        return True, ""
